//
//  CYCarRentalDetailTableVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/23.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYCarRentalDetailTableVC.h"
#import "CYDiscoveryHttpTool.h"
#import "UIInfomationView.h"
#import "MBProgressHUD+MJ.h"
#import "CYCompanyUserTool.h"
#import "MJExtension.h"
#import "CYCarRental.h"
#import "UIImageView+WebCache.h"
#import "CYCarInfo.h"

@interface CYCarRentalDetailTableVC ()
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *idCardLabel;
@property (weak, nonatomic) IBOutlet UIImageView *idCardZimageIView;
@property (weak, nonatomic) IBOutlet UIImageView *idCardFImageView;
@property (weak, nonatomic) IBOutlet UILabel *plaNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *carModelLabel;
@property (weak, nonatomic) IBOutlet UILabel *engineNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *carFrameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *carPhoteIV;
@property (weak, nonatomic) IBOutlet UIImageView *licenseIV;
@property (weak, nonatomic) IBOutlet UILabel *carRentalTime;
@property (weak, nonatomic) IBOutlet UILabel *rzjLabel;
@property (weak, nonatomic) IBOutlet UILabel *yjLabel;
@property (weak, nonatomic) IBOutlet UITextView *bzTV;

@end

@implementation CYCarRentalDetailTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self loadData];
}

- (void)initData {
    if (self.showDropCarItem) {
        UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"还车" style:UIBarButtonItemStylePlain target:self action:@selector(payCarBtn)];
        self.navigationItem.rightBarButtonItem = bar;
    }
    
    if (self.carRental) {
        CYCarRental *car = self.carRental;
        self.nameLable.text = CYStringFormat(@"租车人姓名：%@", car.name);
        self.phoneLabel.text = CYStringFormat(@"手机号码：%@", car.phone);
        self.idCardLabel.text = CYStringFormat(@"身份证号：%@", car.certificateNo);
        if (car.certificateNoZAddress && !car.certificateNoZAddress.isEmpty) {
            [self.idCardZimageIView sd_setImageWithURL:CYURLWithUserService(car.certificateNoZAddress) placeholderImage:[UIImage imageNamed:@"<#name#>"]];
        }
        if (car.certificateNoFAddress && !car.certificateNoFAddress.isEmpty) {
            [self.idCardFImageView sd_setImageWithURL:CYURLWithUserService(car.certificateNoFAddress) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
        }
        self.plaNumLabel.text = CYStringFormat(@"车牌：%@", car.plateNumber);
        self.carModelLabel.text = CYStringFormat(@"车型：%@", @"---");
        self.engineNumLabel.text = CYStringFormat(@"发动机号：%@", @"---");
        self.carFrameLabel.text = CYStringFormat(@"车架号：%@", @"---");
        if (car.phone && !car.phone.isEmpty) {
            [self.carPhoteIV sd_setImageWithURL:CYURLWithUserService(car.photo) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
        }
        if (car.rentType.intValue == 0) {
            self.rzjLabel.text = CYStringFormat(@"日租金：%@", car.rent);
        } else {
            self.rzjLabel.text = CYStringFormat(@"一口价：%@", car.rent);
        }
        self.yjLabel.text = CYStringFormat(@"押金：%@", car.cashPledge);
        self.bzTV.text = car.remark;
    }
}

- (void)payCarBtn {
    [UIInfomationView showAlertViewWithTitle:@"确定还车？" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] clickAtIndex:^(NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [self dropCar];
        }
    }];
}

- (void)dropCar {
    if (self.carRental) {
        [MBProgressHUD showMessage:@"提交中" toView:self.view];
        __block NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [CYCompanyUserTool getUserWithUseType:self.userType user:^(CYUser *user) {
            if (user) {
                param[@"userId"] = user.userId;
                param[@"token"] = user.token;
            }
        } companyUser:^(CYCompanyUserModel *companyUser) {
            if (companyUser) {
                param[@"userId"] = companyUser.carCompanyUserId;
                param[@"token"] = companyUser.token;
            }
        }];
        param[@"carRentId"] = self.carRental.carRentId;
        param[@"carInfoId"] = self.carRental.carInfoId;
        param[@"status"] = @(CYCarStatusTypeDroped);
        
        [CYDiscoveryHttpTool updateStatusWithParam:param success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showSuccess:message];
            [CYNotificationCenter postNotificationName:nCYReloadDataOfCarLeaseAndDrop object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            } else if (error) {
                [MBProgressHUD showError:@"服务器操作失败"];
            }
        }];
    }
}

- (void)loadData {
    if (self.carRental.plateNumber && !self.carRental.plateNumber.isEmpty) {
        [MBProgressHUD showMessage:@"加载中" toView:self.view];
        [CYDiscoveryHttpTool getCarInfoByPlateNumberWithPlateNumber:self.carRental.plateNumber success:^(CYCarInfo *carInfo) {
            [MBProgressHUD hideHUDForView:self.view];
            self.carModelLabel.text = CYStringFormat(@"车型：%@", carInfo.vehicleType);
            self.engineNumLabel.text = CYStringFormat(@"发动机号：%@", carInfo.carEngineNumber);
            self.carFrameLabel.text = CYStringFormat(@"车架号：%@", carInfo.carFrameNumber);
            if (carInfo.vehicleLicence && !carInfo.vehicleLicence.isEmpty) {
                [self.licenseIV sd_setImageWithURL:CYURLWithUserService(carInfo.vehicleLicence) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
            }
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            }
        }];
    }
}
@end
